package com.qyl.honghengshop.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.qyl.honghengshop.dao.BaseDao;
import com.qyl.honghengshop.dao.GoodsTypeDao;
import com.qyl.honghengshop.dto.Goods_type;
import com.qyl.honghengshop.mapper.GoodsMapper;
import com.qyl.honghengshop.mapper.GoodsTypeMapper;
import com.qyl.honghengshop.utils.PageBean;

public class GoodsTypeDaoImpl extends BaseDao implements GoodsTypeDao {

	@Override
	public int getRecordCount(Goods_type goods_type) {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		int count = shopTypeMapper.getRecordCount(goods_type);
		
		this.closeSqlSession();
		
		return count;
	}

	@Override
	public void shopTypeList(PageBean pageBean,Goods_type goods_type) {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		List<Goods_type> list = shopTypeMapper.shopTypeList(pageBean,goods_type);
		
		pageBean.setList(list);
		
		this.closeSqlSession();

	}

	@Override
	public List<Goods_type> getShopTypeAjax() {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		List<Goods_type> list = shopTypeMapper.getShopTypeAjax();
		
		this.closeSqlSession();
		
		return list;
	}

	@Override
	public int add(Goods_type goods_type) {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		//把所有展示栏位与本类型一样的置为0
		shopTypeMapper.setIndexFieldToZero(goods_type.getIndex_field());
		
		//添加商品类型
		int count = shopTypeMapper.add(goods_type);
		
		sqlSession.commit();
		
		this.closeSqlSession();
		
		return count;
	}

	@Override
	public int delShopType(String code) {

		SqlSession sqlSession = this.getSqlSession();
		
		int count = 0;
		
		try {
			//把引用商品类型的外键置为空
			GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
			
			goodsMapper.updateTypeCodeToNull(code);
			
			GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
			
			count = shopTypeMapper.delShopType(code);
			
			sqlSession.commit();			
			
		} catch (Exception e) {
			e.printStackTrace();
			sqlSession.rollback();
		
		}
		this.closeSqlSession();
		
		return count;

	}

	@Override
	public Goods_type getShopTypeByCode(String code) {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		Goods_type goodType = shopTypeMapper.getShopTypeByCode(code);
		
		this.closeSqlSession();
		
		return goodType;
		
	}

	@Override
	public int update(Goods_type goods_type) {
		
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		//把所有展示栏位与本类型一样的置为0
		shopTypeMapper.setIndexFieldToZero(goods_type.getIndex_field());
		
		
		int count = shopTypeMapper.update(goods_type);
		
		sqlSession.commit();
		
		this.closeSqlSession();
		
		return count;
	}

	@Override
	public List<Map<String, String>> getMaxGoodTypes() {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		List<Map<String, String>> maxGoodTypes = shopTypeMapper.getMaxGoodTypes();

		this.closeSqlSession();

		return maxGoodTypes;
	}

	@Override
	public List<Map<String, String>> getMinGoodTypes(String code) {
		
		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		List<Map<String, String>> minGoodTypes = shopTypeMapper.getMinGoodTypes(code);

		this.closeSqlSession();

		return minGoodTypes;
	}

	@Override
	public int getIndexTypeNum() {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		int count = shopTypeMapper.getIndexTypeNum();

		this.closeSqlSession();
		
		return count;
	}

	@Override
	public Map<String, String> getCodeByIndexFiled(int i) {

		SqlSession sqlSession = this.getSqlSession();
		
		GoodsTypeMapper shopTypeMapper = sqlSession.getMapper(GoodsTypeMapper.class);
		
		Map<String, String> map = shopTypeMapper.getCodeByIndexFiled(i);

		this.closeSqlSession();
		
		return map;
	}

}
